package cn.zswltech.gruul.common.dal.query;

import java.io.Serializable;

/**
 * Author: Jason 2018/8/1
 */
public class BaseQuery implements Serializable {

    private static final long serialVersionUID = -8274758873447809336L;

    public static final int DEFAULT_PAGE_SIZE = 10;
    public static final int DEFAULT_PAGE_MAX_SIZE = 5000;

    private int pageSize = DEFAULT_PAGE_SIZE;

    private int startRow = 0;

    private int curPage = 1;

    private int page = curPage;

    private int totalResultCount;

    public int getPageSize() {
        return pageSize;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public void setPageSize(int pageSize) {
        if (pageSize < 1) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        if (pageSize > DEFAULT_PAGE_MAX_SIZE){
            pageSize = DEFAULT_PAGE_MAX_SIZE;
        }
        startRow = (this.page - 1) * pageSize;
        this.pageSize = pageSize;
    }

    public BaseQuery() {
    }

    public BaseQuery(int page, int pageSize) {
        this.page = page;
        this.pageSize = pageSize;
    }

    public int getStartRow() {
        return startRow;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        if (page <= 1) {
            page = 1;
        }
        startRow = (page - 1) * pageSize;
        this.page = page;
    }

    public int getMaxPage() {
        if (totalResultCount <= 1) {
            return 1;
        }
        if (pageSize <= 1) {
            return totalResultCount;
        }
        return (totalResultCount + pageSize - 1) / pageSize;
    }

    public int getPageCount() {
        return getMaxPage();
    }

    public boolean hasNextPage() {
        int currentPage = getCurrentPage();
        return currentPage < getMaxPage();
    }

    public boolean hasPrevPage() {
        int currentPage = getCurrentPage();
        return currentPage > 1;
    }

    public int getNextPage() {
        int currentPage = getCurrentPage();
        if (hasNextPage()) {
            return (currentPage + 1);
        }
        return currentPage;
    }

    public int getPrevPage() {
        int currentPage = getCurrentPage();
        if (hasPrevPage()) {
            return currentPage - 1;
        }
        return currentPage;
    }

    public boolean turnNext() {
        if (hasNextPage()) {
            this.page = getNextPage();
            return true;
        }
        return false;
    }

    public boolean turnPrev() {
        if (hasPrevPage()) {
            this.page = getPrevPage();
            return true;
        }
        return false;
    }

    public boolean turn(int page) {
        int maxPage = getMaxPage();
        if (page > maxPage) {
            this.page = maxPage;
            return false;
        }
        if (page < 1) {
            this.page = 1;
            return false;
        }
        this.page = page;
        return true;
    }

    public int getCurrentPage() {
        int maxPage = getMaxPage();
        if (this.page > maxPage) {
            return maxPage;
        }
        if (maxPage < 1) {
            return 1;
        }
        return this.page;
    }

    public int getTotalResultCount() {
        return totalResultCount;
    }

    public void setTotalResultCount(int totalResultCount) {
        this.totalResultCount = totalResultCount;
        int page = (totalResultCount + pageSize - 1) / pageSize + 1;
        if (this.page > page) {
            this.page = page;
        }
    }

    public int getCurPage() {
        return getCurrentPage();
    }

    public void setCurPage(int curPage) {
        this.page = curPage;
        this.curPage = curPage;
    }
}
